package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kla.power.entity.ScreenMerchantEntity;
import com.kla.power.mapper.ScreenMerchantMapper;
import com.kla.power.service.IScreenMerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.util.SnowflakeIdWorker;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-08-06
 */
@Service
public class ScreenMerchantServiceImpl extends ServiceImpl<ScreenMerchantMapper, ScreenMerchantEntity> implements IScreenMerchantService {


    @Resource
    private ScreenMerchantMapper screenMerchantMapper;

    @Override
    public List<ScreenMerchantEntity> queryList ( ScreenMerchantEntity screenMerchantEntity) {
        if ( screenMerchantEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<ScreenMerchantEntity> screenMerchantEntities = screenMerchantMapper.queryList ( screenMerchantEntity );
        return screenMerchantEntities;
    }

    @Override
    public ScreenMerchantEntity saveSingle (ScreenMerchantEntity screenMerchantEntity) {
        if ( screenMerchantEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        screenMerchantEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( screenMerchantEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return screenMerchantEntity;
    }

    @Override
    public ScreenMerchantEntity updateSingle (ScreenMerchantEntity screenMerchantEntity) {
        if ( screenMerchantEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        // 先查询
        ScreenMerchantEntity screenMerchant = baseMapper.selectOne ( new QueryWrapper<ScreenMerchantEntity> ().eq ( "merchantId", screenMerchantEntity.getMerchantId () ).eq ( "screenId", screenMerchantEntity.getScreenId () ) );
        if ( screenMerchant == null ) {
            screenMerchantEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
            int insert = baseMapper.insert ( screenMerchantEntity );
            if ( insert == 0 ) {
                throw new StateException ( StateEnum.INSERT_EXCE );
            }
            return screenMerchantEntity;
        }
        UpdateWrapper<ScreenMerchantEntity> updateWrapper = new UpdateWrapper<> ();
        if ( screenMerchantEntity.getTitle () != null ) {
            updateWrapper.set ( "title", screenMerchantEntity.getTitle () );
        }
        updateWrapper.eq ( "merchantId", screenMerchantEntity.getMerchantId () ).eq ( "screenId", screenMerchantEntity.getScreenId () );
        screenMerchantEntity.setChangeTime ( new Date () );
        int update = baseMapper.update ( null, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return screenMerchantEntity;
    }

}
